package controllers.useRecord;

import hprose.HproseClinetService;

import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import utils.file.DownExcel;

import com.alibaba.fastjson.JSONObject;
import com.ning.http.util.DateUtil;

import component.UserBis;
import constants.APIConstants;
import constants.Constants;
import controllers.common.filter.BaseFilter;

public class UseRecordAction extends BaseFilter {
	public static void index() {
//		Map<String, Object> useParams = new HashMap<String, Object>();
//		JSONObject useList = new JSONObject();
//		String date = StringUtils.isNotBlank(params.get("date"))?params.get("date"):DateUtils.getCurrentDateTimeSimple();
//		if ("2".equals(UserBis.currType())) {
//			useParams.put("customer_id", UserBis.currUser());
//			useParams.put("operator_id", params.get("operator_id"));
//			useParams.put("date", date);
//			Object[] arguments = new Object[]{useParams};
//			String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSESUMBYCUSTOMERID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
//			useList = JSONObject.parseObject(res);
//		} else if ("3".equals(UserBis.currType())) {
//			useParams.put("operator_id", UserBis.currUser());
//			useParams.put("date", params.get("date"));
//			Object[] arguments = new Object[]{useParams};
//			String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSESUMBYCUSTOMERID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
//			useList = JSONObject.parseObject(res);
//		}
//		render(useList,date);
		render();
	}
	public static void getCutterUseList(){
		Map<String, Object> useParams = new HashMap<String, Object>();
		useParams.put("customer_id", UserBis.currUser());
		useParams.put("operator_id", params.get("operator_id"));
		useParams.put("start_date", params.get("start_date"));
		useParams.put("end_date", params.get("end_date"));
		useParams.put("currPage", params.get("currPage"));
		useParams.put("pageSize", params.get("pageSize"));
		useParams.put("isPage",Constants.DATAISPAGE_YES);
		useParams.put("real_name", params.get("real_name"));
		Object[] arguments = new Object[]{useParams};
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSESUMBYCUSTOMERID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		renderJSON(json);
	}
	/**
	 * 导出领用回收记录
	 * 
	 * 2016年5月6日 下午2:54:06
	 * @auth liqi
	 */
	public static void downCutterUseList(){
		Map<String, Object> useParams = new HashMap<String, Object>();
		useParams.put("customer_id", UserBis.currUser());
		useParams.put("operator_id", params.get("operator_id"));
		useParams.put("start_date", params.get("start_date"));
		useParams.put("end_date", params.get("end_date"));
		useParams.put("real_name", params.get("real_name"));
		useParams.put("isPage", "-1");
		Object[] arguments = new Object[]{useParams};
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSESUMBYCUSTOMERID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		
		List<Map<String,Object>> list = (List<Map<String,Object>>)json.get("data");
        for(int i=0;i<list.size();i++){
        	Map<String,Object> map =list.get(i);
        	int useNum = Integer.parseInt(map.get("useNum").toString());
        	int recNum = Integer.parseInt(map.get("recNum").toString());
        	int holdNum = useNum - recNum;
        	map.put("holdNum", holdNum);
        	list.set(i, map);
        }
        
        HSSFWorkbook wb = DownExcel.downExcel("领用回收记录", "ID:操作员:领用数量:回收数量:持有数量", 
        		"operator_id:real_name:useNum:recNum:holdNum", list);
        // 生成文件名
        String fileName = "UseRecord" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss") + ".xls";
        try {
            response.setContentTypeIfNotSet("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "GB2312"));
            OutputStream out = response.out;
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            list.clear();
        }
	}
	
	public static void getCutterUseListByOperatorId(String operator_id,String start_date,String end_date){
		render(operator_id,start_date,end_date);
	}
	
	public static void ajaxGetCutterUseListByOperatorId(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object[] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("operator_id", params.get("operator_id"));
		map.put("start_date", params.get("start_date"));
		map.put("end_date", params.get("end_date"));
		map.put("currPage", params.get("currPage"));
		map.put("pageSize", params.get("pageSize"));
		map.put("isPage", params.get("isPage"));
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSELISTBYOPERATORID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		renderJSON(json);
	}
	
	/**
	 * 导出领用回收记录详情
	 * 
	 * 2016年5月6日 下午4:32:08
	 * @auth liqi
	 */
	public static void downCutterUseListByOperatorId(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object[] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("operator_id", params.get("operator_id"));
		map.put("start_date", params.get("start_date"));
		map.put("end_date", params.get("end_date"));
		map.put("isPage", "-1");
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSELISTBYOPERATORID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		JSONObject json2 = json.getJSONObject("data");
		List<Map<String,Object>> list = (List<Map<String,Object>>)json2.get("data");
        
        HSSFWorkbook wb = DownExcel.downExcel("领用回收记录详情", "规格简称:规格全称:供应商:领用数量:回收数量", 
        		"simple_specs_cust:long_specs_cust:supp_name:useCount:recCount", list);
        // 生成文件名
        String fileName = "UseRecordDetails" + DateUtil.formatDate(new Date(), "yyyyMMddHHmmss") + ".xls";
        try {
            response.setContentTypeIfNotSet("application/vnd.ms-excel");
            response.setHeader("content-disposition", "attachment;filename="+ URLEncoder.encode(fileName, "GB2312"));
            OutputStream out = response.out;
            wb.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            list.clear();
        }
	}
	/**
	 * 对账
	 * @Title:        billIndex 
	 * @Description:  TODO(这里用一句话描述这个方法的作用) 
	 * @param:        @param startDate
	 * @param:        @param endDate    
	 * @return:       void    
	 * @throws 
	 * @author        caofan
	 * @Date          2015年12月22日 下午3:22:49
	 */
	public static void billIndex(String startDate,String endDate){
		Map<String, Object> map = new HashMap<String, Object>();
		Object[] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERUSEBILLBYCUSTOMERID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		render(json,startDate,endDate);
	}
	
	public static void operatorDetailUseCutterIndex(String operator_id,String start_date,String end_date){
		render(operator_id,start_date,end_date);
	}
	
	public static void operatorDetailUseCutterInfo(){
		Map<String, Object> map = new HashMap<String, Object>();
		Object[] arguments = new Object[]{map};
		map.put("customer_id", UserBis.currUser());
		map.put("operator_id", params.get("operator_id"));
		map.put("start_date", params.get("start_date"));
		map.put("end_date", params.get("end_date"));
		map.put("material_id", params.get("material_id"));
		map.put("supplier_id", params.get("supplier_id"));
		map.put("project_id", params.get("project_id"));
		map.put("process_id", params.get("process_id"));
		map.put("type", params.get("type"));
		map.put("isPage", Constants.DATAISPAGE_YES);
		map.put("currPage", params.get("currPage"));
		map.put("pageSize", params.get("pageSize"));
		String res = HproseClinetService.synCall(APIConstants.URMI_GETCUTTERDETAILBYOPERATORID, arguments,APIConstants.CUSTOMER_HOST, APIConstants.CUSTOMER_PORT);
		JSONObject json = JSONObject.parseObject(res);
		renderJSON(json);
	}
	  
}
